package com.topideal.supplychain.hades.util;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class XmlObject {
    private static final Log log = LogFactory.getLog(XmlObject.class);
    private static final String HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    public static final String REMOVE_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
    private String name;
    private Object value;
    private List<Attribute> attributes;
    private List<XmlObject> subXmlObjects;

    public XmlObject(String name) {
        this.name = name;
    }

    public final String getName() {
        return this.name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public final Object getValue() {
        return this.value;
    }

    public final void setValue(Object value) {
        this.value = value;
    }

    public final void setAttribute(String name, Object value) {
        if (this.attributes == null) {
            this.attributes = new ArrayList<Attribute>();
        }
        Attribute attribute = null;
        for (Attribute att : this.attributes) {
            if (name.equalsIgnoreCase(att.getName())) {
                attribute = att;
                break;
            }
        }
        if (attribute == null) {
            attribute = new Attribute(name, value);
            this.attributes.add(attribute);
        } else {
            attribute.setValue(value);
        }
    }

    public final Object getAttributeValue(String name) {
        return getAttributeValue(name, null);
    }

    public final Object getAttributeValue(String name, Object defaultValue) {
        Attribute attribute = null;
        for (Attribute att : this.attributes) {
            if (name.equalsIgnoreCase(att.getName())) {
                attribute = att;
                break;
            }
        }
        if (attribute == null) {
            return defaultValue;
        }
        return attribute.getValue();
    }

    public final void addSubXmlObject(XmlObject xmlObject) {
        if (this.subXmlObjects == null) {
            this.subXmlObjects = new ArrayList<XmlObject>();
        }
        this.subXmlObjects.add(xmlObject);
    }

    public final String toCompactXml() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + getNoHeadXml("", "");
    }

    public String toFormatXml() {
        return toFormatXml("\t");
    }

    protected final String toFormatXml(String blank) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + getNoHeadXml(blank, "\n");
    }

    protected final String getNoHeadXml(String blank, String split) {
        return getNoHeadXml(blank, split, 0);
    }

    private final String getNoHeadXml(String blank, String split, int count) {
        StringBuffer blanks = new StringBuffer();
        for (int i = 0; i < count; ++i) {
            blanks.append(blank);
        }
        StringBuffer sb = new StringBuffer();
        sb.append(blanks).append("<").append(this.name);
        Iterator<?> iterator;
        if (this.attributes != null) {
            iterator = this.attributes.iterator();
            while (iterator.hasNext()) {
                Attribute attribute = (Attribute) iterator.next();
                sb.append(" " + attribute.getName() + "=\"" + attribute.getValue() + "\"");
            }
        }

        if (this.subXmlObjects == null) {
            if (this.value == null) {
                sb.append("/>").append(split);
            } else {
                sb.append(">");
                sb.append(this.value);
                sb.append("</" + this.name + ">" + split);
            }
        } else {
            sb.append(">").append(split);
            iterator = this.subXmlObjects.iterator();
            ++count;
            while (iterator.hasNext()) {
                XmlObject xmlObject = (XmlObject) iterator.next();
                sb.append(xmlObject.getNoHeadXml(blank, split, count));
            }
            sb.append(blanks + "</" + this.name + ">" + split);
        }
        return sb.toString();
    }

    public static Object XML2Obj(String xmlString, Class<?> clz) {
        Object obj = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {clz});
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            StringReader reader = new StringReader(xmlString);
            obj = unmarshaller.unmarshal(reader);
        } catch (Exception e) {
            log.error("Exception Handler error", e);
        }
        return obj;
    }

    public static String Obj2Xml(Object obj, Class<?> clz) {
        StringWriter sw = new StringWriter();
        String xmlString = null;
        try {
            JAXBContext context = JAXBContext.newInstance(new Class[] {clz});
            Marshaller m = context.createMarshaller();
            m.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
            m.marshal(obj, sw);
            xmlString = sw.toString();
        } catch (Exception e) {
            log.error("Exception Handler error", e);
        } finally {
            try {
                sw.close();
            } catch (Exception e2) {
                log.error("Exception Handler error", e2);
            }
        }
        return xmlString;
    }

    class Attribute {
        private String name;
        private Object value;

        public Attribute(String paramString, Object paramObject) {
            this.name = paramString;
            this.value = paramObject;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Object getValue() {
            return this.value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }
}
